package com.hxzy.dao.Impl;

import com.hxzy.bean.Film;
import com.hxzy.dao.FilmDao;
import com.hxzy.util.JdbcUtils;

import java.util.List;
import java.util.Map;

public class FilmDaoImpl implements FilmDao {
    @Override
    public int save(Film f) {
        String sql="INSERT INTO film (chinese_name,english_name,category,area,duration,`release`) VALUES(?,?,?,?,?,?)";
        Object[] params={f.getChineseName(),f.getEnglishName(),f.getCategory(),f.getArea(),f.getDuration(),f.getRelease()};
        return JdbcUtils.executeUpdate(sql,params);
    }

    @Override
    public int update(Film f) {
        String sql = "UPDATE film f SET f.chinese_name=?,f.english_name=?,f.category=?,f.area=?,f.duration=?,f.`release`=? WHERE f.f_id=?";
        Object[] params={f.getChineseName(),f.getEnglishName(),f.getCategory(),f.getArea(),f.getDuration(),f.getRelease(),f.getfId()};
        return JdbcUtils.executeUpdate(sql,params);
    }

    @Override
    public int remove(Integer integer) {
        String sql ="DELETE FROM film WHERE film.f_id=?";
        return JdbcUtils.executeUpdate(sql,integer);
    }

    @Override
    public List<Map<String, Object>> queryAll() {
        String sql = "SELECT  f_id fId,chinese_name chineseName,english_name englishName,category,area,duration,`release` FROM film";
        return JdbcUtils.executeQuery(sql);
    }

    @Override
    public Film findByID(Integer id) {
        String sql="SELECT f_id fId,chinese_name chineseName,english_name englishName,category,area,duration,`release` from film WHERE film.f_id=?";
        List<Film> films = JdbcUtils.selectSingleTab(Film.class, sql, id);
        if (films == null){
            return null;
        }
        Film film = films.get(0);
        return film;
    }


}
